﻿#include "ComFun.h"
#include<QCoreApplication>

QFile*        CComFun::m_LogFile;
QMutex        CComFun::m_mutex;

CComFun::CComFun(void)
{
}

CComFun::~CComFun(void)
{
}

void CComFun::LogFileInit(){
    QDateTime qDateTime = QDateTime::currentDateTime();
    QString   strLogName = qDateTime.toString("yyyyMMdd_hhmmss") + "_log.txt";

    m_LogFile = new QFile(strLogName);
    m_LogFile->open(QIODevice::WriteOnly | QIODevice::Append);
}
void CComFun::LogFileRelease(){
    m_LogFile->close();
    delete m_LogFile;
}
//打印Log
void  CComFun::OutputMessage(QString msg){
    m_mutex.lock();
    QTextStream text_stream(m_LogFile);
    text_stream << msg << "\r\n";
    m_LogFile->flush();
    m_mutex.unlock();
}

long CComFun::RandomNum( long N )
{
    std::vector<long> vl( N );
    for ( long i=0; i<N; ++i )
    {
        vl[i] = i;
    }
    std::random_shuffle( vl.begin(), vl.end() );
    return (*vl.begin());
}

void CComFun::SleepSecs(int iSec){
    QTime t;
    t.start();
    while(t.elapsed()< iSec * 1000)
        QCoreApplication::processEvents();
}
